<?php

namespace App\Http\Controllers\Api;

use App\Models\Api\Address;
use App\Models\Api\Store;
use App\Models\Api\StoreOrder;
use App\Tools\Financial;
use App\Models\Api\AdminUser;

class UserController extends BaseController
{
    /**
     * 首页
     */
    public function index()
    {
        $store_id = $this->userArr['store_id'];
        $storeInfo = Store::where(['id' => $store_id])->select(['id', 'store_sn', 'agent_start_time', 'agent_end_time'])->first();
        $info['store_sn'] = $storeInfo['store_sn'];
        $info['agent_start_time'] = date('Y-m-d', $storeInfo['agent_start_time']);
        $info['agent_end_time'] = date('Y-m-d', $storeInfo['agent_end_time']);
        $info['hk_money'] = Financial::getInfoByAid($store_id, 1, 1, 'money');
        $info['gl_money'] = Financial::getInfoByAid($store_id, 1, 2, 'money');
        $info['th_money'] = StoreOrder::where(['store_id' => $store_id, 'send_status' => 2])->sum('order_amount');
        successMsg($info);
    }

    /**
     * 商户信息
     */
    public function storeInfo()
    {
        $store_id = $this->userArr['store_id'];
        $storeInfo = Store::getInfoById($store_id);
        successMsg($storeInfo);
    }

    /**
     * 商户信息
     */
    public function storeEdit()
    {
        $pamas = $this->requestArr;
        $store_id = $this->userArr['store_id'];
        $storeInfo = Store::editInfoBySId($store_id, $pamas);
        successMsg($storeInfo);
    }

    /**
     * 用户地址列表
     */
    public function myAddressList()
    {
        $store_id = $this->userArr['store_id'];
        $list = Address::getListBySid($store_id);
        successMsg($list);
    }

    /**
     * 用户地址详情
     */
    public function myAddressDetail()
    {
        $id = $this->requestArr['id'] ?: 0;
        $store_id = $this->userArr['store_id'];
        $info = Address::getInfoById($id, $store_id);
        successMsg($info);
    }

    /**
     * 地址编辑
     */
    public function myAddressEdit()
    {
        $pamas = $this->requestArr;
        $store_id = $this->userArr['store_id'];
        if (empty($pamas['name'])) {
            errorMsg('请填写姓名');
        }
        if (empty($pamas['mobile'])) {
            errorMsg('请填写手机号');
        }
        if (empty($pamas['province']) || empty($pamas['city']) || empty($pamas['area']) || empty($pamas['address'])) {
            errorMsg('请填写完整地址');
        }
        Address::editInfo($store_id, $pamas);
        successMsg();
    }

    /**
     * 地址编辑
     */
    public function myAddressDel()
    {
        $id = $this->requestArr['id'];
        if ($id) {
            $store_id = $this->userArr['store_id'];
            Address::where(['id' => $id, 'client_id' => $store_id])->delete();
            successMsg();
        }
        errorMsg();
    }

    /**
     * 修改密码
     */
    public function changePassword()
    {
        $userId = $this->userArr['id'];
        $password = $this->requestArr['password'];
        $confirmPassword = $this->requestArr['confirmPassword'];

        if (empty($password)) {
            return errorMsg("请输入新密码");
        }
        if (empty($confirmPassword)) {
            return errorMsg("请输入确认密码");
        }
        if ($password != $confirmPassword) {
            return errorMsg("确认密码和新密码不一致");
        }

        $safe = randString();
        $info['safe'] = $safe;
        $info['password'] = md5($password . $safe);

        $result = AdminUser::editAdmin($userId, $info);
        if ($result) {
            return successMsg();
        }
        return errorMsg();
    }
}
